package com.ihr360.expr;

import com.ihr360.expr.core.Expr;
import com.ihr360.expr.core.ExprEvaluatable;
import com.ihr360.expr.core.IExprFunction;
import com.ihr360.expr.engine.BasicEvaluationCallback;
import com.ihr360.expr.utils.ExprUtils;
import junit.framework.TestCase;

public class TH extends TestCase {

    public static void assertResult(String expression, Object result)
            throws Exception {
        assertResult(expression, ExprUtils.convertObject(result));
    }

    public static void assertResult(String expression, Expr result)
            throws Exception {
        assertResult(expression, result, !(result instanceof ExprEvaluatable));
    }

    public static void assertResult(String expression, Expr result, boolean eval)
            throws Exception {
        BasicEvaluationCallback c = new BasicEvaluationCallback();
        assertResult(c, expression, result, eval);
    }

    public static void assertResult(BasicEvaluationCallback c,
                                    String expression, Object result) throws Exception {
        assertResult(c, expression, result,
                !(result instanceof ExprEvaluatable));
    }

    public static void assertResult(BasicEvaluationCallback c,
                                    String expression, Object result, boolean eval) throws Exception {
        Expr e = c.parse(expression);
        if (eval) {
            if (e instanceof ExprEvaluatable) {
                e = ((ExprEvaluatable) e).evaluate(c);
            }
        }
        TestCase.assertEquals(e, ExprUtils.convertObject(result));
    }

    public static Object eval(IExprFunction function, Object... args)
            throws ExprException {
        BasicEvaluationCallback context = new BasicEvaluationCallback();
        return ExprUtils.convertExpr(context, function.evaluate(context, ExprUtils
                .convertArgs(args)));
    }

}